package com.ruoyi.flowable.listener.asset;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquStockin;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.flowable.listener.asset.AssetListener_2.getFileList;
import static com.ruoyi.flowable.listener.asset.AssetListener_3.getApprovalRecord;

/**
 * 验收4、质量性能验收
 */
@Slf4j
@Component("assetListener_4")
public class AssetListener_4 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //外观结果+功能测试结果+性能参数+安全报告编号
            //外观检查
            String waiguan1 = execution.getVariable("waiguan1", String.class);
            String waiguan2 = execution.getVariable("waiguan2", String.class);
            String waiguan3 = execution.getVariable("waiguan3", String.class);
            String detail = execution.getVariable("detail", String.class);
            HashMap<String, Object> waiguanMap = new HashMap<>();
            waiguanMap.put("外观检查", List.of(waiguan1, waiguan2, waiguan3));
            waiguanMap.put("问题描述", detail);
            //功能测试
            String gongneng1 = execution.getVariable("gongneng1", String.class);
            String gongneng2 = execution.getVariable("gongneng2", String.class);
            String gongneng3 = execution.getVariable("gongneng3", String.class);
            String canshu = execution.getVariable("canshu", String.class);
            List<String> fileupload4_2 = getFileList(execution, "fileupload4_2");
            String miaoshu = execution.getVariable("miaoshu", String.class);
            HashMap<String, Object> gongnengMap = new HashMap<>();
            gongnengMap.put("功能测试", List.of(gongneng1, gongneng2, gongneng3));
            gongnengMap.put("参数值", canshu);
            gongnengMap.put("测试异常记录图片", fileupload4_2);
            gongnengMap.put("测试异常描述", miaoshu);
            //安全检查
            String anquan1 = execution.getVariable("anquan1", String.class);
            String anquan2 = execution.getVariable("anquan2", String.class);
            String anquan3 = execution.getVariable("anquan3", String.class);
            String code = execution.getVariable("code", String.class);
            HashMap<String, Object> anquanMap = new HashMap<>();
            anquanMap.put("安全检查", List.of(anquan1, anquan2, anquan3));
            anquanMap.put("安全检查报告编号", code);
            //赋值
            HashMap<String, Object> qualityCheckMap = new HashMap<>();
            qualityCheckMap.put("外观检查", waiguanMap);
            qualityCheckMap.put("功能测试", gongnengMap);
            qualityCheckMap.put("安全检查", anquanMap);
            String qualityCheck = objectMapper.writeValueAsString(qualityCheckMap);

            EquStockin equStockin = execution.getVariable("equStockin", EquStockin.class);

            //附件链接
            String oldFileUrls = equStockin.getFileUrls();
            Map<String, Object> fileUrlsMap = objectMapper.readValue(oldFileUrls, new TypeReference<Map<String, Object>>() {});
            fileUrlsMap.put("测试异常记录图片", fileupload4_2);
            String fileUrls = objectMapper.writeValueAsString(fileUrlsMap);

            //审批记录
            List<Map<String, String>> approvalRecordList = getApprovalRecord(equStockin.getApprovalRecord());
            String approvalRecord = objectMapper.writeValueAsString(approvalRecordList);

            //审批信息
            String approvalInfo = equStockin.getApprovalInfo();
            Map<String, Object> approvalInfoMap = objectMapper.readValue(
                    approvalInfo,
                    new TypeReference<Map<String, Object>>() {});
            approvalInfoMap.put("质量与性能验收", qualityCheckMap);
            approvalInfoMap.put("附件链接", fileUrlsMap);
            approvalInfoMap.put("审批记录", approvalRecordList);
            approvalInfo = objectMapper.writeValueAsString(approvalInfoMap);

            equStockin.setQualityCheck(qualityCheck);
            equStockin.setFileUrls(fileUrls);
            equStockin.setApprovalRecord(approvalRecord);

            equStockin.setApprovalInfo(approvalInfo);

            execution.setVariable("equStockin", equStockin);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
